package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.ruoyi.business.domain.EWallet;
import com.ruoyi.business.service.IEWalletService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.mapper.EDetailMapper;
import com.ruoyi.business.domain.EDetail;
import com.ruoyi.business.service.IEDetailService;

import javax.servlet.http.HttpServletRequest;

/**
 * 账户明细Service业务层处理
 *
 * @author hao
 * @date 2023-02-01
 */
@Service
public class EDetailServiceImpl extends ServiceImpl<EDetailMapper, EDetail> implements IEDetailService {

    @Autowired
    private IEWalletService ieWalletService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询账户明细列表
     *
     * @param eDetail 账户明细
     * @return 账户明细
     */
    @Override
    public List<EDetail> selectEDetailList(EDetail eDetail) {
        List<EDetail> list = getBaseMapper().selectEDetailList(eDetail);
        for (EDetail detail : list) {
            SysUser byAllId = userService.getByAllId(detail.getUserId());
            if (byAllId != null) {
                detail.setUser(byAllId);
            }
            Long userId = SecurityUtils.getUserId();
            SysUser sysUser = userService.getByAllId(userId);
            detail.setNames(sysUser.getNickName());
        }
        return list;
    }

    /**
     * 充值
     *
     * @param eDetail
     * @return
     */
    @Override
    public Boolean congzhiByUserId(EDetail eDetail) {
        //充值
        EWallet eWallet = ieWalletService.getOne(new LambdaQueryWrapper<EWallet>().eq(EWallet::getUserId, eDetail.getUserId()));
        LambdaUpdateWrapper<EWallet> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EWallet::getUserId, eDetail.getUserId())
                .set(EWallet::getCash, eWallet.getCash() + eDetail.getCash())// 充值金额
                .set(EWallet::getIncome, eWallet.getIncome() + eDetail.getCash());// 总收入
        ieWalletService.update(wrapper);
        //明细
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = userService.getByAllId(userId);

        SysUser user = userService.getByAllId(eDetail.getUserId());
        EDetail eDetailData = new EDetail();

        eDetailData.setId(IdWorker.getIdStr());// 编号
        eDetailData.setUser(user); // user用户
        eDetailData.setUserId(eDetail.getUserId()); //userId
        eDetailData.setNames(sysUser.getNickName()); // 当前操作人
        eDetailData.setState(0L); // 类型
        eDetailData.setMoney(eDetail.getCash()); // money
        eDetailData.setRemark(eDetail.getRemark()); // 备注
        eDetailData.setOperateTime(new Date()); // 时间
        return this.save(eDetailData);
    }

    @Override
    public Boolean xiaofeiByUserId(EDetail eDetail) {
        //消费
        EWallet eWallet = ieWalletService.getOne(new LambdaQueryWrapper<EWallet>().eq(EWallet::getUserId, eDetail.getUserId()));
        LambdaUpdateWrapper<EWallet> wrappers = new LambdaUpdateWrapper<>();
        wrappers.eq(EWallet::getUserId, eDetail.getUserId())
                .set(EWallet::getCash, eWallet.getCash() - eDetail.getCash())// 充值金额
                .set(EWallet::getExpenditure, eWallet.getExpenditure() + eDetail.getCash());// 总收入
        ieWalletService.update(wrappers);
        //明细
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = userService.getByAllId(userId);

        SysUser user = userService.getByAllId(eDetail.getUserId());
        EDetail eDetailData = new EDetail();

        eDetailData.setId(IdWorker.getIdStr());// 编号
        eDetailData.setUser(user); // user用户
        eDetailData.setUserId(eDetail.getUserId()); //userId
        eDetailData.setNames(sysUser.getNickName()); // 当前操作人
        eDetailData.setState(1L); // 类型
        eDetailData.setMoney(eDetail.getCash()); // money
        eDetailData.setRemark(eDetail.getRemark()); // 备注
        eDetailData.setOperateTime(new Date()); // 时间
        return this.save(eDetailData);
    }

}
